package com.alibaba.citrus.cr.mn.order.center.facade.adapter.impl;

import com.alibaba.citrus.cr.mn.order.center.facade.adapter.WmsOrderAdapter;
import com.alibaba.cloud.retail.wms.api.model.orders.common.WarehousingOrderDTO;
import com.alibaba.cloud.retail.wms.api.model.orders.request.WmsOrderQueryRequest;
import com.alibaba.cloud.retail.wms.api.model.orders.response.WarehousingOrdersQueryResponse;
import com.alibaba.cloud.retail.wms.api.service.orders.WarehousingOrderReadService;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.cz.base.tool.result.Result;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @Description 库存中心接口适配层
 * @Author dongju
 * @Date 2024/04/12
 */
@Service
public class WmsOrderAdapterImpl implements WmsOrderAdapter {

    private static final Log log = Log.getLogger(WmsOrderAdapterImpl.class);

    @DubboReference
    private WarehousingOrderReadService warehousingOrderReadService;

    @Override
    public List<WarehousingOrderDTO> queryWmsOrderByOtherOrder(String orderBizId) {
        try {
            WmsOrderQueryRequest wmsOrderQueryRequest = new WmsOrderQueryRequest();
            wmsOrderQueryRequest.setBizOrderId(orderBizId);
            wmsOrderQueryRequest.setContainSubOrders(Boolean.TRUE);
            wmsOrderQueryRequest.setProcessQuantity(Boolean.FALSE);
            wmsOrderQueryRequest.setContainDeleted(Boolean.FALSE);
            log.info("WmsOrderAdapterImpl#queryWmsOrderByOtherOrder request:{}", JSON.toJSONString(wmsOrderQueryRequest));
            Result<WarehousingOrdersQueryResponse> result = warehousingOrderReadService.queryWmsOrderByOtherOrder(wmsOrderQueryRequest);
            log.info("WmsOrderAdapterImpl#queryWmsOrderByOtherOrder result:{}", JSON.toJSONString(result));

            if (Objects.isNull(result) || Objects.isNull(result.getResult()) || CollectionUtils.isEmpty(result.getResult().getWarehousingOrders())) {
                return Lists.newArrayList();
            }
            return result.getResult().getWarehousingOrders();
        } catch (Exception e) {
            log.error("WmsOrderAdapterImpl#queryWmsOrderByOtherOrder error", e);
        }
        return Lists.newArrayList();
    }

    @Override
    public List<WarehousingOrderDTO> queryWmsOrderByOtherOrderContainDeleted(String orderBizId) {
        try {
            WmsOrderQueryRequest wmsOrderQueryRequest = new WmsOrderQueryRequest();
            wmsOrderQueryRequest.setBizOrderId(orderBizId);
            wmsOrderQueryRequest.setContainSubOrders(Boolean.TRUE);
            wmsOrderQueryRequest.setProcessQuantity(Boolean.FALSE);
            log.info("WmsOrderAdapterImpl queryWmsOrderByOtherOrderContainDeleted request:{}", JSON.toJSONString(wmsOrderQueryRequest));
            Result<WarehousingOrdersQueryResponse> result = warehousingOrderReadService.queryWmsOrderByOtherOrder(wmsOrderQueryRequest);
            log.info("WmsOrderAdapterImpl queryWmsOrderByOtherOrderContainDeleted result:{}", JSON.toJSONString(result));

            if (Objects.isNull(result) || Objects.isNull(result.getResult()) || CollectionUtils.isEmpty(result.getResult().getWarehousingOrders())) {
                return Lists.newArrayList();
            }
            return result.getResult().getWarehousingOrders();
        } catch (Exception e) {
            log.error("WmsOrderAdapterImpl queryWmsOrderByOtherOrderContainDeleted error", e);
        }
        return Lists.newArrayList();
    }
}
